<template>
  <el-container>
    <el-header>
      <div class="left-panel">
        <fsButton ref="button" plain size="default" :buttons="buttons" @clickHandler="buttonHandler"></fsButton>
      </div>
      <div class="right-panel">
        <ex-search ref="search" :searches="searches" @searchOk="searchHandler" default-code="queryContent" placeholder="请输入内容"></ex-search>
      </div>
    </el-header>
    <el-main class="nopadding">
      <el-container>
        <el-main class="nopadding">
          <ex-table id="interfaceTaskTable" ref="table" :columns="columns" :url="this.$API.system.interfaceTask.list.url" :refs="this.$refs" search="search" @columnClick="columnClickHandler"></ex-table>
        </el-main>
      </el-container>
    </el-main>
  </el-container>
  <view-dialog ref="viewDialog"/>
</template>

<script>
import viewDialog from './view'
import ExTable from "@/components/exTable/index.vue";
import ExSearch from "@/components/exSearch/index.vue";
import BaseTemplate from "@/utils/baseTemplate";

export default {
  name: '接口任务',
  components: {
    viewDialog,
    ExSearch,
    ExTable,
  },
  data() {
    return {
      buttons: [
        // {name: this.$t('button.export'), icon: "el-icon-download", click: "export", permission: "export"},
		  {name: this.$t('button.delete'), icon: "el-icon-delete", click: "del", type: "info", permission: ""},
      ],
      searches: [
        { type: "select", name: "工作类型", code: "taskTypeList", placeholder: "请选择工作类型", dictCode:'EXTERNAL_INTERFACE_TYPE',multiple:true},
        { type: "input", name: "内容", code: "queryContent", placeholder: "请输入内容"},
        { type: "input", name: "发送次数", code: "sendTimes", placeholder: "请输入发送次数"},
		{ type: "select", name: "发送状态", code: "isSuccess", placeholder: "请选择发送状态", dictCode:'TASK_SEND_STATUS'},
		{ type: "select", name: "完成状态", code: "isFinished", placeholder: "请选择完成状态", dictCode:'TASK_FINISHED'},
        { type: "datetimerange", name: "创建时间", code: "createTimeStart,createTimeEnd",startPlaceholder:"请输入开始日期", endPlaceholder: "请输入结束日期", format: "YYYY-MM-DD HH:mm:ss",valueFormat:"YYYYMMDDHHmmss"},

      ],
      columns: [
        {code: "操作", name: "操作", width: "100",click:[
			{label:"查看日志",icon:"el-icon-document",event:'show2',permission: ""}
			]
		},
        {code: "taskType", name: "工作类型", width: "200",click: "show1",formatter:"DICT",key: "EXTERNAL_INTERFACE_TYPE" },
        {code: "sendTimes", name: "发送次数", width: ""},
        {code: "isSuccess", name: "发送状态", width: "200",formatter:"DICT",key: "TASK_SEND_STATUS"},
        {code: "isFinished", name: "完成状态", width: "200",formatter:"DICT",key: "TASK_FINISHED"},
        {code: "isEnabled", name: "启用状态", width: "200",formatter:"DICT",key: "IS_ENABLED"},
        {code: "finishedMemo", name: "任务完成备注", width: "200"},
        {code: "createUser", name: "操作人", width: "100", formatter: "DICT", key: "SysUser"},
        {code: "createTime", name: "操作时间", width: "175", formatter: "DATETIME"},
      ],
      parentLevelCode: null,
    }
  },
  created() {
  },
  mounted() {
  },
  methods: {
    buttonHandler(index, name) {
      this.$options.methods[name](this, index);
    },
    async searchHandler() {
      let queryParams = {pathCode: this.parentLevelCode}
      await this.$refs.table.refresh(queryParams);
    },
    columnClickHandler(name,data){
      this.$options.methods[name](this, data);
    },
    //查看
    show2(_this,row) {
      if (!row) {
        return;
      }
	  let queryParams = {taskId:row.id};
	  _this.searchTaskLog(row.id,queryParams);
    },
    //查看
    async show1(_this,row) {
     var data={};
     data.id=row.id;
      //请求数据加载
      var res = await _this.$API.system.interfaceTask.findById.post({id:row.id});
      if (res.code === 2000) {
        row.content=res.data.content;
      }

      _this.$nextTick(() => {
        _this.$refs.viewDialog.open(row);
      })
    },
	  //批量删除
	  del(_this, index) {
		  let selections = _this.$refs.table.selections();
		  if (selections.length === 0) {
			  _this.$message.warning(_this.$t("common.please_select_data"));
			  return;
		  }
		  _this.$confirm(`确定删除选中的 ${selections.length} 项及其下面的接口任务日志吗？`, _this.$t('common.warn'), {
			  type: 'warning'
		  }).then(async () => {
			  _this.$refs.button.loading(index, true);
			  var delarray = [];
			  for (let index = 0; index < selections.length; index++) {
				  const element = selections[index];
				  delarray.push(element.id);
			  }
			  var res = await _this.$API.system.interfaceTask.deleteByIds.post(delarray);
			  if (res.code === 2000) {
				  _this.$message.success(_this.$t("code."+res.code));
				  _this.searchHandler();
				  _this.$store.dispatch("commit",selections[0].entityName);
			  }
			  _this.$refs.button.loading(index, false);
		  }).catch(() => {
			  _this.$refs.button.loading(index, false);
		  });
	  },
    //导出
    async export(_this,index) {
      let attachParams = {};
      let exportApi = _this.$API.system.interfaceTask.export;
      let exTableRef = _this.$refs.table;
      let exportBtnRef = _this.$refs.button;
      let exportBtnIndex = index;
      await BaseTemplate.export(_this, exportApi, attachParams, exportBtnRef, exportBtnIndex, exTableRef);
    },
	  searchTaskLog(item,queryParams) {
		  let title = "接口任务日志";
		  //跳转到搜索台账页面
		  let fromRouteId = this.$TOOL.crypto.MD5(this.$route.path);//当前页面的路由地址
		  let key = this.$TOOL.crypto.MD5(item+title);//可以做为key的关键字段或内容
		  /**
		   *  1 = 根据管理部门，根据登录账户管理范围，资产的管理部门在账户管理范围内的显示<br>
		   *  2 = 根据使用部门，根据登录账户管理范围，资产的使用部门在账户管理范围内的显示<br>
		   *  3 = 根据管理部门或使用部门，根据登录账户管理范围，资产的使用部门或管理部门在账户管理范围内的显示<br>
		   *  其它 = 根据管理部门，按台账，根据使用部门或管理部门或借用部门在管理范围内的
		   */
			  //设置搜索参数
		  let routeParams = {
				  id: key,
				  title: title,
				  fixedParams:{
					  taskId:queryParams.taskId,
				  },//固定搜索参数，打开页面后，用户通过搜索界面的清空按钮无法清除
			  };
		  this.$store.dispatch("addRouteParams", routeParams);
		  this.$router.push({
			  name: "toInterfaceTaskLog",
			  params: {fromRouteId: fromRouteId, key: key}
		  });
	  },
  }
}
</script>
